/**
 * 
 */

// do not overwrite buffer if initialized earlier
report_buffer = window.report_buffer || {};

var report_dialog_close_connector = null;

function show_report(coordinates, moon, showInline, inTooltip, html_id) {
	if (moon != true && moon != false) return;
	var check = coordinates.match(/\d+\:\d+\:\d+/);
	
	var key = coordinates;
	if (moon) key += "M";
	
	if (check) {
		// continue with coordinates
		coordinates+"M";
		
		// check buffer
		if (typeof report_buffer[key] != "undefined") {
			// buffered
			display_report_dialog(coordinates, moon, inTooltip, html_id);
		} else {
			// not in our buffer -> load into buffer
			// display will happen async.
			var xhrArgs = {
			        url: "ajax/ajax_report.php?type=load&coordinates="+coordinates+"&moon="+moon,
			        handleAs: "json",
			        load: function(jsonData) {
			        	if (jsonData != "" && "undefined" != typeof jsonData.id) {
			        		report_buffer[key] = jsonData;
			        		if (showInline === undefined) {
			        			display_report_dialog(coordinates, moon, inTooltip, html_id);
			        		} else {
			        			var content =  galaxytool.report_display.display(resources_on_text, report_buffer[key]["planetname"], coordinates, "", report_when, report_uploaded_by, report_buffer[key]["username"], report_buffer[key], moon, true, galaxytool_report_archive_title, galaxytool_total_res_text);
			        			dojo.html.set(dojo.byId("ReportInline"), content);
			        		}
			        	}
			        },
			        error: function(error) {
						console.log("Error:");
						console.log(error);
			        }
			}	
			dojo.xhrPost(xhrArgs);
		}
	}
}

function display_report_dialog(coordinates, moon, inTooltip, html_id) {
	if (moon != true && moon != false) return;
	var pos = coordinates.match(/\d+\:\d+\:(\d+)/)[1];
	
	var key = coordinates;
	if (moon) key += "M";

	if (typeof report_buffer[key] == "undefined") {
		// not buffered?
		console.log("error: buffer empty for coordinates");
		return;
	}
	
	// load content from buffer
	var content = galaxytool.report_display.display(resources_on_text, report_buffer[key]["planetname"], coordinates, "", report_when, report_uploaded_by, report_buffer[key]["username"], report_buffer[key], moon, true, galaxytool_report_archive_title, galaxytool_total_res_text);
	if (inTooltip === true) {
		var ar;
		var or;
		if (html_id === undefined) {
			// default for galaxy view
			ar = moon ? dojo.byId("reports1_" + pos) : dojo.byId("reports2_" + pos);
			// prefer vertical alignment on galaxy - because reports are in a column
			or = ["below", "below-alt", "above", "above-alt"]
		} else {
			// everywhere else, e.g. player information / ally information
			ar = dojo.byId(html_id);
			// prefer horizontal alignment on player/allyinformation - because coordinates are in one line
			or = ["after", "before", "below", "below-alt", "above", "above-alt" ];
		}
		
		dojo.byId("reporttooltipcontent").innerHTML = content;
		
		dijit.popup.open({ popup: dijit.byId("reporttooltip"), around: ar, orient: or });
		window.setTimeout(galaxytool.report_display.CloseReportTooltipIfNotUsed, 2000); // close popup if mouse is not using it within 2 seconds
	} else {
		// close the tooltip, which might still be open
		galaxytool.report_display.CloseReportTooltip();
		
		var dialog = dijit.byId("ReportDialog");
		dialog.set("title",galaxytool_report_archive_details);
		dialog.set("content", content);
		dialog.show();	
		
		galaxytool.report_display.addReportClickAndCloseListener();
	}
}